package com.jhz.speedy.component.sound;

import android.annotation.SuppressLint;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

import com.jhz.speedy.R;
import com.jhz.speedy.common.util.SharedUtils;
import com.jhz.speedy.component.control.ConstantKeys;

import java.util.HashMap;

/**
 * 声音播放器
 * Created by JunHeZ on 2017/7/19.
 */
public class PlaySound {
    private static final String TAG = PlaySound.class.getSimpleName();

    public static final int SOUND_TYPE_LOCK = 1;    // 锁车
    public static final int SOUND_TYPE_UNLOCK = 2;  // 开锁
    public static final int SOUND_TYPE_CONNET = 3;      // 连接
    public static final int SOUND_TYPE_DISCONNET = 4;   // 连接断开
    public static final int SOUND_TYPE_KEY = 5;     // 按钮
    public static final int SOUND_TYPE_MUNE = 6;
    public static final int SOUND_TYPE_START = 7;   // 启动
    public static final int SOUND_TYPE_SHUT_DOWN = 8;    // 熄火
    public static final int SOUND_TYPE_TRUNK = 9;   // 尾箱
    public static final int SOUND_TYPE_FINDE = 10;   // 寻车

    private static PlaySound playSound;

    private AudioManager mAudioManager;   // 声音管理器
    @SuppressLint("UseSparseArrays")
    private HashMap<Integer, Integer> soundMap; // 声音集合
    private SoundPool soundPool;    // 声音池

    private int lastPlayId;  // 当前播放的id

    private PlaySound(Context context) {
        load(context);
    }

    public static PlaySound getInstance(Context context) {
        if (playSound == null) {
            playSound = new PlaySound(context);
        }

        return playSound;
    }

    /**
     * 加载
     */
    public void load(Context context) {
        if(soundMap != null && soundMap.size() > 0) {
            return; // 已有就不再加载了
        }

        mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        soundPool = new SoundPool(200, AudioManager.STREAM_SYSTEM, 0); // 创建一个SoundPool对象，该对象可以容纳9个音频流

        // 将声音一次读取，存在缓存中
        soundMap = new HashMap();
        soundMap.put(SOUND_TYPE_LOCK, soundPool.load(context, R.raw.lock, 1));
        soundMap.put(SOUND_TYPE_UNLOCK, soundPool.load(context, R.raw.unlock, 1));
        soundMap.put(SOUND_TYPE_CONNET, soundPool.load(context, R.raw.connet, 1));
        soundMap.put(SOUND_TYPE_DISCONNET, soundPool.load(context, R.raw.disconnet, 1));
        soundMap.put(SOUND_TYPE_KEY, soundPool.load(context, R.raw.key, 1));
        soundMap.put(SOUND_TYPE_MUNE, soundPool.load(context, R.raw.mune, 1));
        soundMap.put(SOUND_TYPE_START, soundPool.load(context, R.raw.start, 1));
        soundMap.put(SOUND_TYPE_SHUT_DOWN, soundPool.load(context, R.raw.stop, 1));
        soundMap.put(SOUND_TYPE_TRUNK, soundPool.load(context, R.raw.trunk, 1));
        soundMap.put(SOUND_TYPE_FINDE, soundPool.load(context, R.raw.finde, 1));
    }

    /**
     * 播放
     *
     * @param soundType 声音类型
     */
    public void play(int soundType) {
        boolean isPlay = false;
        switch (soundType){
            case SOUND_TYPE_LOCK:   // 锁车
                isPlay = ! SharedUtils.getInstance().getBoolean(ConstantKeys.SOUND_SET_CLOSE_LOCK);
                break;

            case SOUND_TYPE_UNLOCK:   // 开锁
                isPlay = ! SharedUtils.getInstance().getBoolean(ConstantKeys.SOUND_SET_CLOSE_UNLOCK);
                break;

            case SOUND_TYPE_START:   // 启动
                isPlay = ! SharedUtils.getInstance().getBoolean(ConstantKeys.SOUND_SET_CLOSE_START);
                break;

            case SOUND_TYPE_SHUT_DOWN:   // 熄火
                isPlay = ! SharedUtils.getInstance().getBoolean(ConstantKeys.SOUND_SET_CLOSE_SHUT_DOWN);
                break;

            case SOUND_TYPE_FINDE:   // 寻车
                isPlay = ! SharedUtils.getInstance().getBoolean(ConstantKeys.SOUND_SET_CLOSE_FIND_CAR);
                break;
        }

        if(! isPlay){
            return;
        }

        int currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);     // 当前声音
        soundPool.play(soundMap.get(soundType), currentVolume, currentVolume, 0, 0, 1); // 播放指定的音频
    }


    /**
     * 释放
     */
    public void close() {
        soundPool.release();
        soundPool = null;
    }
}
